package com.travel.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.group.common.PageResult;
import com.travel.group.entity.Forum;
import com.travel.group.entity.GroupWxuser;
import com.travel.group.entity.WxUsers;
import com.travel.group.service.ForumService;
import com.travel.group.service.GroupWxuserService;
import com.travel.group.service.WxUserService;
import com.travel.group.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author Xudaoxu
 * @create 2021-05-26 2021/5/26
 */
@Controller
public class ForuController {
    @Autowired
    private ForumService forumService;

    @Autowired
    private GroupWxuserService groupWxuserService;

    @Resource
    private WxUserService wxUserService;

    @Resource
    private GroupWxuserService wxuserService;

    @RequestMapping("/forum/toPage")
    public String toPage(){
        return "forum/list";
    }

    @RequestMapping("/wx/addForumInfo")
    @ResponseBody
    public Object addForumInfo(@RequestBody Forum forum){
        forum.setStatus(0);
        if (forumService.save(forum))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    @RequestMapping("/wx/getAllForumInfo")
    @ResponseBody
    public Object getAllForumInfo(HttpServletRequest request){
        String userId = request.getParameter("userId"); //除了自己发布的信息以外的信息

        QueryWrapper<Forum> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Forum> list = forumService.list(wrapper);
        for (int i = 0; i < list.size(); i ++) {

        }

        return WeChatResponseUtil.ok(list);
    }

    @RequestMapping("/wx/getAllMyForumInfo")
    @ResponseBody
    public Object getAllMyForumInfo(HttpServletRequest request){
        String userId = request.getParameter("userId");
        QueryWrapper<Forum> wrapper = new QueryWrapper<>();
        wrapper.eq("publisher", userId);
        List<Forum> list = forumService.list(wrapper);

        for (Forum forum : list){
            QueryWrapper<GroupWxuser> groupWxuserWrapper = new QueryWrapper<>();
            groupWxuserWrapper.eq("group_id", forum.getId());
            List<GroupWxuser> groupWxuserList = groupWxuserService.list(groupWxuserWrapper);
            forum.setNumber(groupWxuserList.size());
        }
        return WeChatResponseUtil.ok(list);
    }

    //单独查询所用的接口
    @RequestMapping("/forum/queryList")
    @ResponseBody
    public PageResult<Forum> queryList(Integer page, Integer limit, @RequestParam("name") String name) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Forum> allArticle = forumService.getAllGroupInfoPlus(page, limit, name);
        Integer count = allArticle.size();
        PageResult<Forum> res = new PageResult<>(allArticle, count);
        return res;
    }

    @RequestMapping("/forum/query")
    @ResponseBody
    public PageResult<Forum> list(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Forum> allArticle = forumService.getAllGroupInfo(page, limit);
        Integer count = forumService.countData();
        PageResult<Forum> res = new PageResult<>(allArticle, count);
        return res;
    }

    @RequestMapping("/forum/delete/{id}")
    @ResponseBody
    public PageResult delete(@PathVariable("id") Integer id){
        if(forumService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }

    @RequestMapping("/forum/check/{id}")
    @ResponseBody
    public PageResult check(@PathVariable("id") Integer id){
        Forum forum = forumService.getById(id);
        forum.setStatus(1);
        if(forumService.updateById(forum)){
            return new PageResult("审核成功",200);
        }
        return new PageResult("审核失败",400);
    }

    //同意用户是否参加该活动
    @RequestMapping("/wx/agreeForum")
    @ResponseBody
    public PageResult agreeForum(@RequestBody GroupWxuser wxuser){
        QueryWrapper<GroupWxuser> groupQueryWrapper = new QueryWrapper<>();
        groupQueryWrapper.eq("id", wxuser.getId());
        GroupWxuser group1 = new GroupWxuser();
        group1.setStatus(wxuser.getStatus());

        if(wxuserService.update(group1, groupQueryWrapper)){
            return new PageResult("审核成功",200);
        }
        return new PageResult("审核失败",400);
    }

    //获取我们所有的申请信息
    @RequestMapping("/wx/getMyForumApplyInfo")
    @ResponseBody
    public Object getMyGroupApplyInfo(HttpServletRequest request){
        String userId = request.getParameter("userId"); //除了自己发布的信息以外的信息
        QueryWrapper<Forum> wrapper = new QueryWrapper<>();
        wrapper.eq("publisher", userId);
        wrapper.eq("status", 1);
        List<Forum> list = forumService.list(wrapper);

        for (Forum forum : list){
            QueryWrapper<GroupWxuser> groupWxuserWrapper = new QueryWrapper<>();
            groupWxuserWrapper.eq("group_id", forum.getId());
            groupWxuserWrapper.eq("status",0);
            List<GroupWxuser> groupWxuserList = groupWxuserService.list(groupWxuserWrapper);

            for (GroupWxuser groupWxuser : groupWxuserList) {
                QueryWrapper<WxUsers> groupWxuserQueryWrapper = new QueryWrapper<>();
                groupWxuserQueryWrapper.eq("user_id", groupWxuser.getWxuserId());
                WxUsers users = wxUserService.getOne(groupWxuserQueryWrapper);
                groupWxuser.setWxUsers(users);
            }

            forum.setList(groupWxuserList);
            forum.setNumber(groupWxuserList.size());

        }
        return WeChatResponseUtil.ok(list);
    }
}
